package com.fingard.dsp.bank.directbank.cdcb01;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

import java.io.UnsupportedEncodingException;

/**
 * @author: liuxb
 * @create: 2020-01-02 11:47
 * @version: 1.0
 **/
public class CDCB01Base extends DirectBase {
    protected static final String INSERTSEQ = "INSERT INTO REQSEQIDMAPPING(URID,BANKCODE,TRANSDATE,REQSEQID,BANKREQSEQID) values (?,?,?,?,?);";
    protected static final String SELECTSEQ = "SELECT BANKREQSEQID FROM REQSEQIDMAPPING WHERE REQSEQID=? AND BANKCODE=?;";

    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    protected void writeCommonHeader(XmlTextWriter xmlWriter, CDCB01Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.serviceId)) {
            throw new Exception("未赋值serviceId");
        }
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("stream");
        xmlWriter.writeElementString("TRANSCODE", p_bankHeader.serviceId);

    }

    /**
     * 发送银行前置
     */
    protected String[] sendToBank(String pReqStr) throws UnsupportedEncodingException {
        WriteBankLogStep2(pReqStr);
        String[] retStr = new String[]{"", ""};
        String tmpAddrUrl = bankFront.serverURL;
        WriteBankLogLn("2.提交到银行的地址：" + tmpAddrUrl);
        WebRequest tmpWebRequest = new WebRequest(tmpAddrUrl);
        tmpWebRequest.setConnectTimeout(60000);
        tmpWebRequest.setRequestProperty("Method", "POST");
        tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        tmpWebRequest.setRequestProperty("connection", "close");// 设置连接状态
        byte[] tmpUpBytes = pReqStr.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());
        WriteBankLogStep3("3.银行返回的报文：" + retStr[1]);
        return retStr;
    }


}
